package com.example.imtoken.modules.main.wallet.manage.admin.pass_tips

import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.imtoken.R
import com.example.imtoken.core.base.BaseActivity
import com.example.imtoken.databinding.ActivityPassWordTipsBinding

/**
 * 密码提示
 */
class PassWordTipsActivity :
    BaseActivity<PassWordTipsViewModel, ActivityPassWordTipsBinding>(PassWordTipsViewModel::class.java) {

    private val key by lazy {
        intent.getStringExtra("key")
    }

    private var isShowPassTips = false

    override fun initLayoutId(): Int {
        return R.layout.activity_pass_word_tips
    }

    override fun init() {
        dataBinding.viewModel = viewModel
        initTitle()
        initView()
        initClick()
        initObserve()
        if (key == null){
            viewModel.getDefAccount()
        }else{
            viewModel.getDefAccount(key)
        }
    }

    private fun initView() {
        dataBinding.etPassWordTips.hint = getString(R.string.passwordTips)
        if (isShowPassTips) {
            dataBinding.etPassWordTips.isEnabled = true

            dataBinding.etPassWordTips.transformationMethod =
                HideReturnsTransformationMethod.getInstance()

            dataBinding.ivShowPass.setImageResource(R.mipmap.ic_password_show)
        } else {
            dataBinding.etPassWordTips.isEnabled = false

            dataBinding.etPassWordTips.transformationMethod =
                PasswordTransformationMethod.getInstance()

            dataBinding.ivShowPass.setImageResource(R.mipmap.ic_password_no_show)

        }
        isShowPassTips = !isShowPassTips
    }


    private fun initObserve() {
        viewModel.passTips.observe(this) {
            viewModel._passTips = it
        }
        viewModel.isSuccess.observe(this) {
            if (it) {
                dataBinding.include17.tvHeadFunction.setTextColor(ColorUtils.getColor(R.color.welcomeBgColor))
            } else {
                dataBinding.include17.tvHeadFunction.setTextColor(ColorUtils.getColor(R.color.loginTextColorDef))
            }
        }
    }

    private fun initClick() {
        dataBinding.include17.ivHeadBack.setOnClickListener { finish() }
        dataBinding.ivShowPass.setOnClickListener {
            initView()
        }
        dataBinding.include17.tvHeadFunction.setOnClickListener {
            if (viewModel.isSuccess.value == true) {
                if (viewModel.savePassTips()) {
                    ToastUtils.showShort(R.string.passwordTipsSuccess)
                    finish()
                } else {
                    ToastUtils.showShort(R.string.passwordTipsError)
                }
            }
        }
    }

    private fun initTitle() {
        dataBinding.include17.tvHeadTitles.text = getString(R.string.passwordTips)
        dataBinding.include17.tvHeadFunction.visibility = View.VISIBLE
        dataBinding.include17.tvHeadFunction.text = getString(R.string.finish)
        dataBinding.include17.tvHeadFunction.setTextColor(ColorUtils.getColor(R.color.loginTextColorDef))
    }
}